package com.hxxy.wyp.petsLover;

import com.hxxy.wyp.petsLover.common.entities.vo.AjaxResult;
import com.hxxy.wyp.petsLover.common.entities.vo.AjaxSuccess;
import com.hxxy.wyp.petsLover.exception.AppException;
import org.omg.CORBA.portable.ApplicationException;

public class RestBase {
    /**
     * return common format error code for AJAX calls
     *
     * @param errorCode client side error code
     */
    protected AjaxResult<String> ajaxErrorCode(String errorCode) {
        AjaxResult<String> ae = new AjaxResult<>();
        ae.setCode(errorCode);
        return ae;
    }

    /**
     * return common format error for AJAX calls
     * <p>
     * this method will directly return error message to client side, client side js does not need to register error to $app.errors
     */
    protected AjaxResult<String>  ajaxError(String error) {
        AjaxResult<String> ae = new AjaxResult<>();
        ae.setMessage(error);
        return ae;
    }

    protected AjaxResult<String>  ajaxError(String code,String msg) {
        AjaxResult<String> ae = new AjaxResult<>();
        ae.setCode(code);
        ae.setMessage(msg);
        return ae;
    }

    /**
     * handle exception and return proper message to client
     * <p>
     * if exception is instance of ApplicationException, then the error message will be returned to client
     * otherwise the error code 'system_error' will be returned
     */
    protected AjaxResult<String>  ajaxError(Exception exception) {
        if (ApplicationException.class.isAssignableFrom(exception.getClass()))
            return ajaxError(exception.getMessage());

        return ajaxError("System Error");
    }



    /**
     * simply return a success notification with extra data.
     */
    protected <T>  AjaxResult<T> ajaxSuccess(T data) {
        AjaxResult<T> result = new AjaxResult<>();

        result.setData(data);
        return result;
    }

    protected <T>  AjaxResult<T> ajaxSuccess(T data,String code) {
        AjaxResult<T> result = new AjaxResult<>();

        result.setData(data);
        result.setCode(code);
        return result;
    }

    protected <T>  AjaxResult<T> ajaxSuccess(String code ,String msg) {
        AjaxResult<T> result = new AjaxResult<>();
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }
    /**
     * simply return a success notification with extra data.
     */
    protected <T>  AjaxResult<T> ajaxSuccess(T data,String code,String message) {
        AjaxResult<T> result = new AjaxResult<>();

        result.setData(data);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * simply return a success notification without any extra data
     */
    protected AjaxSuccess ajaxSuccess() {
        AjaxSuccess as = new AjaxSuccess();
        as.setReturnCode(1);
        return as;
    }

    /**
     * 抛出非法数据的错误
     */
    protected void throwInvalidData() {
        throw new AppException(ServiceBase.getResource(ServiceBase.ERROR_INVALID_DATA));
    }

    /**
     * 抛出非法操作的错误
     */
    protected void throwInvalidOperation() {
        throw new AppException(ServiceBase.getResource(ServiceBase.ERROR_INVALID_OPERATION));
    }
}